#include "COfficeGMMgr.h"
#include "MsgOfficeGM.pb.h"
#include "commmsg.h"
#include "commclass.h"
#include "commerrno.h"
#include "../mainctrl.h"

int COfficeGMMgr::FreezeRole(CPlayerBaseInfo& stPlayerBase,
        MsgReqOfficeGM& stReq)
{
    int iSubCmd = stReq.sub_type();
    int iDayNum = 0;
    int iFreezeLimit = 0;
    if (stReq.datas_size() <= 0)
    {
        return ERROR_OFFICE_GM_LESS_ARGUMENT;
    }
    
    iDayNum = stReq.datas(0);
    if (iDayNum <= 0)
    {
        return ERROR_OFFICE_GM_ERROR_ARGUMENT;
    }

#define MAX_OFFICE_GM_FREEZE_DAY 999
    if (iDayNum > MAX_OFFICE_GM_FREEZE_DAY)
    {
        iDayNum = MAX_OFFICE_GM_FREEZE_DAY;
    }

    iFreezeLimit = iDayNum * 86400 + CMainCtrl::m_pstMain->m_iNow;

    switch (iSubCmd)
    {
    case ID_GM_OFFICE_SUB_FREEZE_LOGIN: //以天为单位冻结玩家登录
        {
            stPlayerBase._stFreezeData._iLoginLimit = iFreezeLimit;
        }
        break;
    case ID_GM_OFFICE_SUB_FREEZE_SPEAK: //以天为单位冻结玩家发言
        {
            stPlayerBase._stFreezeData._iSpeakLimit = iFreezeLimit;
        }
        break;
    case ID_GM_OFFICE_SUB_FREEZE_GOLD: //以天为单位冻结使用金币
        {
            stPlayerBase._stFreezeData._iGoldLimit = iFreezeLimit;
        }
        break;
    case ID_GM_OFFICE_SUB_FREEZE_SILVER: //以天为单位冻结使用银币
        {
            stPlayerBase._stFreezeData._iSilverLimit = iFreezeLimit;
        }
        break;
    case ID_GM_OFFICE_SUB_FREEZE_BAG_STORGE: //以天为单位冻结背包和仓库
        {
            stPlayerBase._stFreezeData._iBagLimit = iFreezeLimit;
            stPlayerBase._stFreezeData._iStorgeLimit = iFreezeLimit;
        }
        break;
    default:
        return ERROR_OFFICE_GM_ERROR_ARGUMENT;
    }

    return 0;
}

int COfficeGMMgr::UnFreezeRole(CPlayerBaseInfo& stPlayerBase,
        MsgReqOfficeGM& stReq)
{
    int iSubCmd = stReq.sub_type();

    switch (iSubCmd)
    {
    case ID_GM_OFFICE_SUB_UNFREEZE_LOGIN: //解冻玩家登录限制
        {
            stPlayerBase._stFreezeData._iLoginLimit = 0;
        }
        break;
    case ID_GM_OFFICE_SUB_UNFREEZE_SPEAK: //解冻玩家发言限制
        {
            stPlayerBase._stFreezeData._iSpeakLimit = 0;
        }
        break;
    case ID_GM_OFFICE_SUB_UNFREEZE_GOLD: //解冻玩家使用金币限制
        {
            stPlayerBase._stFreezeData._iGoldLimit = 0;
        }
        break;
    case ID_GM_OFFICE_SUB_UNFREEZE_SILVER: //解冻玩家使用银币限制
        {
            stPlayerBase._stFreezeData._iSilverLimit = 0;
        }
        break;
    case ID_GM_OFFICE_SUB_UNFREEZE_BAG_STORGE: //解冻玩家背包和仓库限制
        {
            stPlayerBase._stFreezeData._iBagLimit = 0;
            stPlayerBase._stFreezeData._iStorgeLimit = 0;
        }
        break;
    default:
        return ERROR_OFFICE_GM_ERROR_ARGUMENT;
    }

    return 0;
}

int COfficeGMMgr::SendRoleGift(CPlayerBaseInfo& stPlayerBase,
        MsgReqOfficeGM& stReq)
{
    int iRet = 0;
    //stReq.datas数据格式:礼包ID 礼包数量 礼包ID 礼包数量 礼包ID 礼包数量...
    if (stReq.datas_size() <= 0)
    {
        return ERROR_OFFICE_GM_LESS_ARGUMENT;
    }
    if (stReq.datas_size() % 2)
    {
        return ERROR_OFFICE_GM_LESS_ARGUMENT;
    }

    int iGiftNum = 0;
    unsigned int uiGiftID = 0;
    for (int i = 0; i < stReq.datas_size(); i+=2)
    {
        uiGiftID = (unsigned int)stReq.datas(i);

        if (i + 1 < stReq.datas_size())
        {
            iGiftNum = stReq.datas(i + 1);
            if (iGiftNum <= 0)
            {
                return ERROR_OFFICE_GM_ERROR_ARGUMENT;
            }

            if (!CMainCtrl::m_pstMain->m_stItemMgr.IsGiftValid(uiGiftID))
            {
                CT_ERROR(("office gm send role(%u) gift(%u) but conf id error!",
                    stPlayerBase._uiGID,uiGiftID));
                return ERROR_GIFT_CONFIG_NONE;
            }
        }
    }

    for (int i = 0; i < stReq.datas_size(); i+=2)
    {
        uiGiftID = (unsigned int)stReq.datas(i);

        if (i + 1 < stReq.datas_size())
        {
            iGiftNum = stReq.datas(i + 1);
            if (iGiftNum <= 0)
            {
                return ERROR_OFFICE_GM_ERROR_ARGUMENT;
            }

            iRet = CMainCtrl::m_pstMain->m_stItemMgr.DropBonus(OSS_MODULE_GM,
                stPlayerBase._uiGID, uiGiftID, iGiftNum);
            if (iRet)
            {
                CT_ERROR(("office gm send role(%u) gift(%u) num(%d) error(%d)",
                    stPlayerBase._uiGID, uiGiftID, iGiftNum, iRet));
            }
        }
    }

    CMainCtrl::m_pstMain->m_stOssMgr.TriggerGMSendGift(stPlayerBase._uiGID,
        stReq);

    return 0;
}

int COfficeGMMgr::SendSysNotice(MsgReqOfficeGM& stReq)
{
    if (!stReq.has_name() && stReq.datas_size() <= 0)
    {
        return ERROR_OFFICE_GM_LESS_ARGUMENT;
    }

    MsgTips stTips;
    CTSizeString<ROLENAME_LEN> szNameStr;
    stTips.set_type(TIPS_SEND_SYSTEM_NOTICE);

    if (stReq.has_name())
    {
        stTips.add_tip_argument(stReq.name());
    }

    for (int i = 0; i < stReq.datas_size(); ++i)
    {
        szNameStr.Set("%d",stReq.datas(i));
        stTips.add_tip_argument(szNameStr());
    }

    CMainCtrl::m_pstMain->SendOnlineTips(stTips);
    CMainCtrl::m_pstMain->FlushSendCache();

    return 0;
}

int COfficeGMMgr::SysStopNotice(MsgReqOfficeGM& stReq)
{
    int iRet = 0;
    
    //复用这两个字段 rolename表示停机时间 usertoken表示开机时间
    if (!stReq.has_rolename() || !stReq.has_usertoken())
    {
        return ERROR_OFFICE_GM_LESS_ARGUMENT;
    }

    int iSegStopTime = 0;
    int iSegStartTime = 0;
    iRet = CMainCtrl::m_pstMain->m_stActMgr.ParseTime(stReq.rolename(),
        iSegStopTime, CActCfgData::ACT_CFG_SEG_TIME);
    CT_RETURN(iRet);

    iRet = CMainCtrl::m_pstMain->m_stActMgr.ParseTime(stReq.usertoken(),
        iSegStartTime, CActCfgData::ACT_CFG_SEG_TIME);
    CT_RETURN(iRet);

    if (iSegStopTime == iSegStartTime)//表示清掉停机设置
    {
        CMainCtrl::m_pstMain->m_stActMgr.ClearSystemStopTime();
        return 0;
    }

    int iDayTime = (CMainCtrl::m_pstMain->m_stTm.tm_sec +
                    CMainCtrl::m_pstMain->m_stTm.tm_min * 60 +
                    CMainCtrl::m_pstMain->m_stTm.tm_hour * 3600);

    if (iSegStopTime <= iDayTime)//做这样的限制 停机时刻是今天未来时刻
    {
        return ERROR_OFFICE_GM_ERROR_ARGUMENT;
    }

    CMainCtrl::m_pstMain->m_stActMgr.SetSysStopTime(
        CMainCtrl::m_pstMain->m_iDayZeroTime + iSegStopTime);

    CMainCtrl::m_pstMain->m_stActMgr.SetSysStopTime(stReq.rolename().c_str());
    CMainCtrl::m_pstMain->m_stActMgr.SetSysStartTime(stReq.usertoken().c_str());

    return 0;
}

